<?php

class TomrepairApi extends CI_Controller
{

    private $user;
    private $token;
    private $headers;
    private $baseurl;

    function __construct()
    {
        parent::__construct();
        $this->load->service('wms/maintain_shop_service');
        $this->load->service('wms/shop_maintain_product_service');
        $this->load->service('wms/request_tools_service');
        $this->user = 'api@tomrepair.es';
        $this->token = '78de933bd0fbacf02ef2de1116e3338f1c627b07';
        $headers[] = 'Authorization:DMCOHh3Y6Zf562MaDU3YzMyRjp3RFP9987trzdLZRZzcjFt';
        $headers[] = 'Content-Type: application/json; charset=utf-8';
        $this->headers = $headers;
        if (ENVIRONMENT == 'development')
        {
            $this->baseurl = 'http://test.dhhtgroup.com:8099/api/index.php/';
        }
        else
        {
            $this->baseurl = 'http://www.tomrepair.es/api/index.php/';
        }
        echo  $this->baseurl;
    }

    function sync_shop_brand()
    {
        $url = $this->baseurl . "addStoresSku";
        $data_to_post = array();
        $data_to_post['user'] = $this->user;
        $data_to_post['token'] = $this->token;
        $data = array();
        $rs = $this->shop_maintain_product_service->get_shop_maintain_product();
//        var_dump($rs);exit;
        foreach ($rs as $value)
        {
            $data[] = array(
                'napa_stores_id' => $value['shop_id'],
                'type_id' => $value['type_id'],
                'brand_id' => $value['brand_id'],
                'model_id' => $value['model_id'],
                'status' => $value['active']
            );
        }
        $data_to_post['product'] = $data;
//                var_dump($data_to_post);exit;
        $params = json_encode($data_to_post);
        $result = $this->request_tools_service->post_htts($url, $params, $this->headers);
        echo ($result);
        exit;
    }

    function sync_shop_info()
    {
        $data = array();
        $shopInfo = $this->maintain_shop_service->get_mshop();
        foreach ($shopInfo as $value)
        {
            $data[] = array(
                'shop_id' => $value['id'],
                'shop_name' => $value['shop_name'],
                'join_time' => '2016-12-27 11:28:' . rand(10, 59),
                'day_limit' => $value['day_max_order'],
                'week_limit' => $value['week_max_order'],
            );
        }
        $url = $this->baseurl . "addStores";
        $data_to_post = array();
        $data_to_post['user'] = $this->user;
        $data_to_post['token'] = $this->token;
        $data_to_post['shop'] = $data;
//        echo '<pre>';
//        var_dump($data_to_post);
        $params = json_encode($data_to_post);
        $result = $this->request_tools_service->post_htts($url, $params, $this->headers);
        echo ($result);
        exit;
    }

}

?>
